package com.shell.governor.user.rx;

import com.shell.bee.base.utils.StringUtil;
import com.shell.bee.base.utils.SystemUtil;
import com.shell.bee.entity.auth.AuthOrgan;
import com.shell.framework.rx.OnErrorNotImplementedSubscriber;
import com.shell.framework.utils.CommonDataUtil;
import com.shell.framework.utils.SysDateUtil;
import com.shell.governor.modules.service.PubDictionaryService;
import com.shell.governor.user.UserEvent;
import com.shell.governor.user.dao.PubUserDao;
import com.shell.governor.user.dao.PubUserExtraDao;
import com.shell.governor.user.po.PubUser;
import com.shell.governor.user.po.PubUserExtra;
import com.shell.governor.user.po.PubUserOpt;
import com.shell.governor.user.service.PubUserService;
import com.shell.governor.user.vo.PubUserVo;
import com.shell.governor.util.InviteCodeUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class UpdateUserSubscriber extends OnErrorNotImplementedSubscriber<UserEvent> {
	@Autowired
	PubDictionaryService dictionaryService;
	@Autowired
	PubUserDao pubUserDao;
	@Autowired
	PubUserExtraDao pubUserExtraDao;
	@Autowired
	PubUserService pubUserService;

	public void onApplicationEvent(UserEvent event) {
		PubUserVo userVo = event.getUserVo();
		PubUserOpt userOpt = event.getUserOpt();
		boolean needApprover = needApprover(userVo);
		PubUser user = new PubUser();

		BeanUtils.copyProperties(userVo, user);
		if (StringUtil.emptyAndNull(user.getUserId())) {
			user.setUserId(SystemUtil.getUUID());

			user.setCreatorId(CommonDataUtil.getUserId());
			user.setCreateTime(SysDateUtil.getSystemDateTime());
			this.pubUserDao.insert(user);
		} else {
			PubUser oldUser = (PubUser) this.pubUserDao.selectByPrimaryKey(user.getUserId());
			user.setCreatorId(oldUser.getCreatorId());
			if (StringUtil.emptyAndNull(oldUser.getPassword())) {
				this.pubUserDao.updateByPrimaryKeySelective(user);
			}
		}
		PubUserExtra userExtra = new PubUserExtra();
		userExtra.setEntId(CommonDataUtil.getCurEntId());
		userExtra.setUserId(user.getUserId());
		this.pubUserExtraDao.delete(userExtra);

		AuthOrgan organ = CommonDataUtil.getCurEnt();
		userExtra.setInviteCode(InviteCodeUtil.genCode(organ.getOrganCode()));
		if (needApprover) {
			userExtra.setUserState(userOpt.getCurState());
			userExtra.setGrpId(getGrpId());
		} else {
			userExtra.setUserState("E");
			if (!this.pubUserService.hasDefaultUser(user.getUserId())) {
				this.pubUserService.cleanDefaultUser(user.getUserId());
				userExtra.setIsDefault("1");
			} else {
				userExtra.setIsDefault("0");
			}
		}
		if (StringUtil.notEmpty(userVo.getAuthFileIds())) {
			userExtra.setAuthFileRlatId(SystemUtil.getUUID());
		}
		if (StringUtil.empty(userOpt.getUserId())) {
			userOpt.setUserId(userExtra.getUserId());
		}
		if (StringUtil.empty(userOpt.getEntId())) {
			userOpt.setEntId(userExtra.getEntId());
		}
		this.pubUserExtraDao.insert(userExtra);

		event.setNeedApprover(needApprover);
		event.setUserExtra(userExtra);
		event.setUser(user);
	}

	private boolean needApprover(PubUserVo user) {
		String cusType = CommonDataUtil.getCurEnt().getCusType();
		if ((StringUtil.empty(cusType)) || ("FINNER".equals(cusType))) {
			return false;
		}
		if (StringUtil.emptyAndNull(getGrpId())) {
			return false;
		}
		if (StringUtil.notEmpty(user.getUserId())) {
			PubUserExtra userExtra = new PubUserExtra();
			userExtra.setEntId(CommonDataUtil.getCurEntId());
			userExtra.setUserId(user.getUserId());
			userExtra = (PubUserExtra) this.pubUserExtraDao.selectOne(userExtra);
			if ((userExtra != null) && ("2".equals(userExtra.getUserState()))) {
				return true;
			}
		}
		if ((StringUtil.emptyAndNull(user.getUserId())) || ((StringUtil.notEmpty(user.getUserId()))
				&& (StringUtil.notEmpty(user.getRoleCodes())) && (user.getRoleCodes().indexOf("ENT_ADMIN") >= 0))) {
			return true;
		}
		return false;
	}

	private String getGrpId() {
		PubUserExtra userExtra = new PubUserExtra();
		userExtra.setUserId(CommonDataUtil.getUserId());
		userExtra.setEntId(CommonDataUtil.getCurEntId());
		userExtra = (PubUserExtra) this.pubUserExtraDao.selectOne(userExtra);
		if ((userExtra != null) && (StringUtil.notEmpty(userExtra.getGrpId()))) {
			return userExtra.getGrpId();
		}
		return null;
	}
}
